package vip.xls8z.reflect.learning;

import vip.xls8z.reflect.learning.pojo.User;

import java.lang.reflect.Field;
import java.util.stream.Stream;

public class ReflectTests {
    public static void main(String[] args) throws Exception {
        Class<User> clazz = User.class;

        // 获取父类的所有属性(private/public/protected)
        Field[] superDeclaredFields = clazz.getSuperclass().getDeclaredFields();
        Stream.of(superDeclaredFields).forEach(System.out::println);

        System.out.println("---------------------------------------");

        // 获取当前类的所有属性(private/public/protected)
        Field[] declaredFields = clazz.getDeclaredFields();
        Stream.of(declaredFields).forEach(System.out::println);

        System.out.println("---------------------------------------");

        // 只能拿到 public 字段(当前类和父类)
        Field[] fields = clazz.getFields();
        Stream.of(fields).forEach(System.out::println);

        System.out.println("---------------------------------------");

        // 无参构造函数创建对象
        User user = clazz.getConstructor().newInstance();
        System.out.println("Empty Constructor Created: " + user);

        System.out.println("---------------------------------------");

        // 有参构造函数创建对象
        User jason = clazz.getConstructor(String.class, String.class).newInstance("Jason", "24");
        System.out.println("Non-empty Constructor Created: " + jason);

    }
}
